package com.huanbao.nongwu.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.huanbao.common.api.CommonResult;
import com.huanbao.common.exception.BusinessException;
import com.huanbao.nongwu.pojo.AreaVO;
import com.huanbao.nongwu.pojo.SysUserVO;
import com.huanbao.nongwu.pojo.beans.AdminArea;
import com.huanbao.nongwu.pojo.beans.Area;
import com.huanbao.nongwu.service.AreaServiceImpl;
import com.huanbao.nongwu.service.SewageServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2021/3/24 15:18
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@RestController
@RequestMapping("areas")
public class AreaController {


    @Autowired
    private AreaServiceImpl iAreaService;

    @Autowired
    private SewageServiceImpl iSewageService;


    /**
     * 获取  区域
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getareas")
    public CommonResult showAreas(int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        PageInfo<SysUserVO> pageInfo = iAreaService.getAreasByPaging(map);
        return CommonResult.success(pageInfo);
    }

    /**
     * 新增区域
     */
    @RequestMapping("/addnewarea")
    public CommonResult addNewArea(@RequestBody AreaVO areaVO)  {
        Area area = new Area();
        BeanUtils.copyProperties(areaVO, area);

        int areaCount = iAreaService.insertSelective(area);

        //添加管理员 地区关系表记录
        AdminArea adminArea = new AdminArea();
        adminArea.setAdminid(Integer.valueOf(areaVO.getAdminId()));
        adminArea.setAreaid(area.getId());
        int adminAreaCount = iAreaService.insert(adminArea);

        if (areaCount != 1 && adminAreaCount != 1) {
            throw new BusinessException("添加地区失败");
        }
        return CommonResult.success();
    }

    /**
     * 删除 区域
     */
    @RequestMapping("/deleteseletedareas")
    public CommonResult deleteSeletedAdmin(@RequestBody AreaVO areaVO) {
        List<Integer> idList = areaVO.getSelectedIds();

        //已经绑定地区id
        List<Integer> hasBindingAreas = new ArrayList<Integer>();
        for (int i = 0; i < idList.size(); i++) {
            //检查 是不是有污水站点跟他关联？
            int sewageCount = iSewageService.getSewageCountLinktoAreaBy(idList.get(i));
            if (sewageCount > 0) {
                hasBindingAreas.add(idList.get(i));
                //不能 删除 因为 已经有污水站点绑定！
            } else {
                int count = iAreaService.deleteByPrimaryKey(idList.get(i));
                int count2 = iAreaService.deleteAdminAreaByAreaId(idList.get(i));
                if (count != 1 && count2 != 1) {
                    throw new BusinessException("删除地区失败！");
                }

            }
        }

        return CommonResult.success();
    }


}
